package com.per.mybootall.controller;


import com.per.mybootall.aop.example2.PermissionAnnotation;
import com.per.mybootall.common.Const;
import com.per.mybootall.pojo.UserPo;
import com.per.mybootall.service.LoginInter;

import com.per.mybootall.utils.RandomValidateCodeUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yuan
 */

@RestController
@RequestMapping("testShiro")
@Api(value = "desc of class")
public class LoginController {
    @Autowired
    private LoginInter loginInter;
    @Autowired
    private RandomValidateCodeUtil randomValidateCode;
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);


    @ApiOperation(value = "desc of method", notes = "")
    @GetMapping("/login")
    @ResponseBody
    @PermissionAnnotation()
    public Map<String, Object> loging(UserPo userpo) {

        Map<String, Object> map = loginInter.login(userpo.getUsername(), userpo.getPassword(), userpo.getPhotomessahes(), userpo.getUuid());
        return map;
    }

    @ApiOperation(value = "这是一个API接口", notes = "register")
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, String> register(String username, String password, String photouuid, int yanzma) {
        System.out.println(username + ";" + password + ";" + photouuid + ";" + yanzma);
        Map<String, String> map = new HashMap<String, String>();
        if (nameExist(username)) {
            map = loginInter.register(username, password, photouuid, yanzma);
            return map;
        }
        map.put("msg", Const.nameIsExist.EXIST);
        return map;
    }

    @ApiOperation(value = "这是一个API接口", notes = "判断是否存在")
    @RequestMapping(value = "/nameExist", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String nameisExist(String username) {
        String msg = null;
        System.out.println(username + ";username");
        if (nameExist(username)) {
            msg = Const.nameIsExist.EXIST;
            return msg;
        }
        msg = Const.nameIsExist.NO_EXIST;
        return msg;
    }

    /*判断是否存在*/
    public boolean nameExist(String username) {
        System.out.println(username + ";username");
        String msg = loginInter.nameisexist(username);
        if (Const.nameIsExist.EXIST.equals(msg)) {
            //存在
            return true;
        } else {
            return false;
        }
    }


    /**
     * 生成验证码
     */
    @ApiOperation(value = "这是一个API接口", notes = "输出验证码图片方法")
    @RequestMapping(value = "/getVerify")
    @ResponseBody
    public Map<String, Object> getVerify() {
        Map<String, Object> map = new HashMap<String, Object>();
        map = randomValidateCode.getRandcode();//输出验证码图片方法
        return map;
    }

    @ApiOperation(value = "这是一个API接口", notes = "进入getPhono")
    @RequestMapping(value = "getPhono")
    @ResponseBody
    public Map<String, String> getPhono(String photo) throws JSONException {
        System.out.println("进入getPhono");
        Map<String, String> map = loginInter.getPhoto(photo);
        return map;
    }

    @GetMapping("testGet")
    @ResponseBody
    public String testGet(HttpServletResponse response) throws IOException {
        //纯粹的原生poi处理复杂样式
        XSSFWorkbook workbook = excelCarrierOrder();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        workbook.write(os);
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "0");
        response.setHeader("charset", "utf-8");
        // 当前日期，用于导出文件名称
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String name = sdf.format(new Date()) + ".xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(name, "UTF-8") + "\"");
        workbook.write(response.getOutputStream());
        if (null != os) {
            os.close();
        }
        //return new ResponseEntity<Resource>(null, null, HttpStatus.OK);
        return "ok";
    }

    public XSSFWorkbook excelCarrierOrder() throws IOException {

        //模板路径
        String path = "/templates/carOut.xlsx";

        XSSFWorkbook workboot = new XSSFWorkbook(this.getClass().getResourceAsStream(path));
        XSSFSheet sheetAt = workboot.getSheetAt(0);
        List<HashMap> list = new ArrayList<>();
        HashMap map = new HashMap();
        map.put("序号", "序号");
        map.put("车牌", "车牌");
        map.put("出场时间", "出场时间");
        map.put("车类", "车类");
        map.put("出场亭岗", "出场亭岗");
        map.put("出场通道", "出场通道");
        map.put("停车场", "停车场");
        list.add(map);
        list.add(map);
        list.add(map);
        list.add(map);
        //开始创建行和表格并且加入数据
        //行
        XSSFRow row;
        int i = 2;
        XSSFCell xssfCell = sheetAt.createRow(0).createCell(0);
        xssfCell.setCellValue("出场记录表  出场时间:11:11:11");


        XSSFCellStyle cellStyle = (XSSFCellStyle) workboot.createCellStyle();

        cellStyle.setFillBackgroundColor(new XSSFColor(new java.awt.Color(156, 195, 230)));
        cellStyle.setBorderBottom(BorderStyle.THIN);//下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        xssfCell.setCellStyle(cellStyle);


        XSSFCellStyle cellStyleoTher = (XSSFCellStyle) workboot.createCellStyle();

        cellStyleoTher.setFillBackgroundColor(new XSSFColor(new java.awt.Color(156, 195, 230)));
        cellStyleoTher.setBorderBottom(BorderStyle.THIN);//下边框
        cellStyleoTher.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyleoTher.setBorderRight(BorderStyle.THIN);//右边框


        sheetAt.createRow(1).createCell(0).setCellValue("导出人:导出人  导出时间:时间");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        for (HashMap dto : list) {

            i += 1;
            //创建行
            row = sheetAt.createRow(i);

            if (null != dto.get("序号").toString()) {
                XSSFCell cell = row.createCell(0);
                cell.setCellValue(dto.get("序号").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("车牌")) {
                XSSFCell cell = row.createCell(1);
                cell.setCellValue(dto.get("车牌").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("出场时间")) {
                XSSFCell cell = row.createCell(2);
                cell.setCellValue(dto.get("出场时间").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("车类")) {
                XSSFCell cell = row.createCell(3);
                cell.setCellValue(dto.get("车类").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("出场亭岗")) {
                XSSFCell cell = row.createCell(4);
                cell.setCellValue(dto.get("出场亭岗").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("出场通道")) {
                XSSFCell cell = row.createCell(5);
                cell.setCellValue(dto.get("出场通道").toString());
                cell.setCellStyle(cellStyleoTher);
            }

            if (null != dto.get("停车场")) {
                XSSFCell cell = row.createCell(6);
                cell.setCellValue(dto.get("停车场").toString());
                cell.setCellStyle(cellStyleoTher);
            }
        }
        return workboot;
    }

}
